using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Mail;
using System.Text;

namespace CustomerContactSystem.Core.Infrastructure.Email {
    public class BaseEmail {
        public string Recipient { get; set; }
        public string SentFrom { get; set; }
        public string Subject { get; set; }
        public bool IsHtml { get; set; }

        protected StringBuilder _messageContent;
        public string MessageContent {
            get {
                return (_messageContent != null) ? _messageContent.ToString() : "";
            }
        }

        private string _filePaths = "";
        private List<Attachment> _attachments;
        public ReadOnlyCollection<Attachment> Attachments {
            get {
                return (_attachments != null) ? _attachments.AsReadOnly() : null;
            }
        }
        public string AttachmentsString {
            get { return _filePaths.Trim(','); }
        }

        private List<MailAddress> _ccAddresses;
        public ReadOnlyCollection<MailAddress> CcAddresses {
            get {
                return (_ccAddresses != null) ? _ccAddresses.AsReadOnly() : null;
            }
        }
        public string CcAdressesString {
            get {
                var returnList = "";
                if ((_ccAddresses != null)) {
                    returnList = _ccAddresses.Aggregate(returnList, (current, addy) => current + (addy.Address + ","));
                }
                return returnList.Trim(',');
            }
        }

        private List<MailAddress> _bccAddresses;
        public ReadOnlyCollection<MailAddress> BccAddresses {
            get {
                return (_bccAddresses != null) ? _bccAddresses.AsReadOnly() : null;
            }
        }
        public string BccAdressesString {
            get {
                var returnList = "";
                if ((_bccAddresses != null)) {
                    returnList = _bccAddresses.Aggregate(returnList, (current, addy) => current + (addy.Address + ","));
                }
                return returnList.Trim(',');
            }
        }

        public void AddAttachment(string filePath) {
            if (_attachments == null) {
                _attachments = new List<Attachment>();
            }
            var a = new Attachment(filePath);
            _attachments.Add(a);
            _filePaths += filePath + ",";
        }

        public void AddCcAddress(string emailAddress) {
            var ccAddress = new MailAddress(emailAddress);
            if (_ccAddresses == null) {
                _ccAddresses = new List<MailAddress>();
            }
            _ccAddresses.Add(ccAddress);
        }

        public void AddBccAddress(string emailAddress) {
            var addy = new MailAddress(emailAddress);
            if (_bccAddresses == null) {
                _bccAddresses = new List<MailAddress>();
            }
            _bccAddresses.Add(addy);
        }

        public void Write(string messageText) {
            if (_messageContent == null) {
                _messageContent = new StringBuilder();
            }
            _messageContent.Append(messageText);
        }

        public void WriteLineToBody(string messageText) {
            if (_messageContent == null) {
                _messageContent = new StringBuilder();
            }
            _messageContent.Append("<p>" + messageText + "</p>");
            _messageContent.Append(IsHtml ? "<br>" : System.Environment.NewLine);
        }

        public void WriteHtmlLinkToBody(string linkUrl, string linkText) {
            if (_messageContent == null) {
                _messageContent = new StringBuilder();
            }
            _messageContent.Append("<a href=\"" + linkUrl + "\" title=\"" + linkText + "\">" + linkText + "</a>");
        }
    }
}